{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b5045b2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "a=3\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16253b5f",
   "metadata": {},
   "source": [
    "# pytjon练习\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "651d8e94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.444\n"
     ]
    }
   ],
   "source": [
    "f_=3.444\n",
    "print(f_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e83cf70b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, '3r3r']\n",
      "dwdwdwdwd =3r3r\n"
     ]
    }
   ],
   "source": [
    "line=[2,3,4,\"3r3r\"]\n",
    "print(line)\n",
    "print(\"dwdwdwdwd ={}\".format(line[3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "deb8697f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "halOllow\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "time.sleep(2)\n",
    "print(\"halOllow\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34d787ed",
   "metadata": {},
   "source": [
    "#### 基本数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "499af2ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'str'> <class 'bool'> "
     ]
    }
   ],
   "source": [
    "x=10\n",
    "y=20.3\n",
    "z=\"dwww\"\n",
    "g=True\n",
    "print(type(x),end=' ')  #end=' ' 取消换行\n",
    "print(type(y),end=' ')\n",
    "print(type(z),end=' ')\n",
    "print(type(g),end=' ')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79959308",
   "metadata": {},
   "source": [
    "#### 导入库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9bb944ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------hal-------------\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "import time      # 导入所有\n",
    "from time import sleep  # 导入sleep函数\n",
    "print(\"-----------------hal-------------\")\n",
    "for i in range(10):\n",
    "    print(i)\n",
    "    sleep(1)  # 休眠1秒\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4a8778aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adwdawawdw = True\n",
      " P=False\n",
      " =None\n"
     ]
    }
   ],
   "source": [
    "s=True\n",
    "f=False\n",
    "g=None\n",
    "print (\"adwdawawdw = {}\\n P={}\\n ={}\" .format(s,f,g) )\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54cc498b",
   "metadata": {},
   "source": [
    "#### 逻辑运算符\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ab8587df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Both a and b are true\n",
      "Both a and b are true\n",
      "Either a or b is false\n"
     ]
    }
   ],
   "source": [
    "\n",
    "a=5\n",
    "\n",
    "b=3\n",
    "if a and b :\n",
    "    print(\"Both a and b are true\")\n",
    "else:\n",
    "    print(\"Either a or b is false\")\n",
    "\n",
    "if a or b :\n",
    "    print(\"Both a and b are true\")\n",
    "else:\n",
    "    print(\"Either a or b is false\")\n",
    "if (a not in [1,2,3]) and (b not in [1,2,3]) :\n",
    "    print(\"Both a and b are true\")\n",
    "else:\n",
    "    print(\"Either a or b is false\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "145a2343",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import time\n",
    "for i in range(10):\n",
    "    time.sleep(1)\n",
    "    print(i)\n",
    "\n",
    "\n",
    "try:\n",
    "    for i in range(10):\n",
    "        time.sleep(1)\n",
    "        print(i)\n",
    " \n",
    "except:\n",
    "    print(\"Error\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1003e22",
   "metadata": {},
   "source": [
    "#### 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "fe4128d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "green\n",
      "14\n",
      "[[12, 14, 24, 36]]\n",
      "[78, 90, 100, 120]\n",
      "['red', 'green', 'blue', 'sadad']\n",
      "['red', 'green', 'sadad']\n"
     ]
    }
   ],
   "source": [
    "list =[\"red\",\"green\",\"blue\"]\n",
    "print(list[1])\n",
    "laa=[[12,14,24,36],[45,56,446,67],[78,90,100,120]]\n",
    "print(laa[0][1])  \n",
    "print(laa[0:1])\n",
    "print(laa[-1])\n",
    "list.append(\"sadad\")\n",
    "print(list)\n",
    "del list[2]\n",
    "print(list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "488b7f75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is 3\n"
     ]
    }
   ],
   "source": [
    "a=3\n",
    "match a:\n",
    "    case 1:\n",
    "        print(\"a is 1\")\n",
    "    case 2:\n",
    "        print(\"a is 2\")\n",
    "    case 3:\n",
    "        print(\"a is 3\")\n",
    "    case 4:\n",
    "        print(\"a is 4\")\n",
    "    case _:\n",
    "        print(\"a is not 1, 2, 3 or 4\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5626d00f",
   "metadata": {},
   "source": [
    "#### 推导式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "69b21dfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "a=(x for x in range(10) if x%2==0 )   # 有一个变量，有一个变量的取值，和一系列判断\n",
    "s=(t for t in range(10) if t%6==0 )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "84c20e17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 6)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tuple(a)\n",
    "tuple(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a634745c",
   "metadata": {},
   "source": [
    "#### 迭代器与生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d598f740",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n",
      "3 3\n",
      "44 55\n",
      "66 77\n",
      "88 99\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "list=[1,2,3,3,44,55,66,77,88,99]\n",
    "iter_list=iter(list)    #   iter() 和 next()。为迭代器可便利列表中的元素。\n",
    "for i in iter_list:\n",
    "    print(i, end=' ');print(next(iter_list)) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc2e2979",
   "metadata": {},
   "source": [
    "#### 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "b9ecaef3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World!\n"
     ]
    }
   ],
   "source": [
    "def hello_world( ss,bb):\n",
    "    if(ss==bb):\n",
    "        print(\"Hello, World!\")\n",
    "\n",
    "\n",
    "hello_world(1,1)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c71419e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# 像是宏函数一样的作用\n",
    "\n",
    "sum =lambda x,y:x+y\n",
    "print(sum(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "99a7f6d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hellonolllwwwwllll, World!\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def helloss_world(func):\n",
    "    def helloss_world():\n",
    "        if(1!=1):\n",
    "            print(\"Hellonolllllll, World!\")\n",
    "        else:\n",
    "            print(\"Hellonolllwwwwllll, World!\")\n",
    "        func()\n",
    "    return helloss_world\n",
    "@helloss_world\n",
    "def function_name():\n",
    "    # function body\n",
    "    if(1==2):\n",
    "        print(\"Hello, World!\")\n",
    "\n",
    "function_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "5f5c951b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def before_call_code():\n",
    "    print(\"dfdwdwdwd\")\n",
    "# code to be executed before the function call\n",
    "    pass\n",
    "\n",
    "def after_call_code():\n",
    "    print(\"dfefefefefefefe\")   \n",
    "# code to be executed after the function call        \n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "565751b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def decorator_function(original_function):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        # 这里是在调用原始函数前添加的新功能\n",
    "        before_call_code()\n",
    "        \n",
    "        result = original_function(*args, **kwargs)\n",
    "        \n",
    "        # 这里是在调用原始函数后添加的新功能\n",
    "        after_call_code()\n",
    "        \n",
    "        return result\n",
    "    return wrapper\n",
    "\n",
    "# 使用装饰器\n",
    "@decorator_function\n",
    "def target_function(arg1, arg2):\n",
    "    pass  # 原始函数的实现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19c2a745",
   "metadata": {},
   "source": [
    "#### 不定长参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "7cdd69a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4)\n",
      "{'name': 'John', 'age': 30}\n",
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "def hello_world(*args, **kwargs ):\n",
    "    print(args)\n",
    "    print(kwargs)\n",
    "    print(\"Hello World!\")\n",
    "hello_world(1,2,3,4, name=\"John\", age=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6fb1738",
   "metadata": {},
   "source": [
    "#### 定义装饰器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c0dbec2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dfdwdwdwd\n",
      "arg1: 1 arg2: 1\n",
      "dfefefefefefefe\n",
      "dfdwdwdwd\n",
      "arg1: 1 arg2: 2\n",
      "dfefefefefefefe\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function __main__.decorator_function.<locals>.wrapper(*args, **kwargs)>"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 定义装饰器\n",
    "def decorator_function(original_function):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        # 这里是在调用原始函数前添加的新功能\n",
    "        before_call_code()\n",
    "        \n",
    "        result = original_function(*args, **kwargs)\n",
    "        \n",
    "        # 这里是在调用原始函数后添加的新功能\n",
    "        after_call_code()\n",
    "        \n",
    "        return result\n",
    "    return wrapper\n",
    "\n",
    "# 使用装饰器\n",
    "@decorator_function\n",
    "def target_function(arg1, arg2):\n",
    "    print(\"arg1:\", arg1, \"arg2:\", arg2)\n",
    "    pass  # 原始函数的实现\n",
    "\n",
    "target_function(1, 1)\n",
    "decorator_function(target_function(1,2)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "192e99ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['_STRUCT_TM_ITEMS',\n",
       " '__doc__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__spec__',\n",
       " 'altzone',\n",
       " 'asctime',\n",
       " 'ctime',\n",
       " 'daylight',\n",
       " 'get_clock_info',\n",
       " 'gmtime',\n",
       " 'localtime',\n",
       " 'mktime',\n",
       " 'monotonic',\n",
       " 'monotonic_ns',\n",
       " 'perf_counter',\n",
       " 'perf_counter_ns',\n",
       " 'process_time',\n",
       " 'process_time_ns',\n",
       " 'sleep',\n",
       " 'strftime',\n",
       " 'strptime',\n",
       " 'struct_time',\n",
       " 'thread_time',\n",
       " 'thread_time_ns',\n",
       " 'time',\n",
       " 'time_ns',\n",
       " 'timezone',\n",
       " 'tzname']"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8565db2e",
   "metadata": {},
   "source": [
    "#### 模块导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "774396fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, world!\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    print(\"Hello, world!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "871672ae",
   "metadata": {},
   "source": [
    "#### 输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "035b9a08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You entered: 饿e\n"
     ]
    }
   ],
   "source": [
    "a=input(\"Enter a number: \")\n",
    "print(\"You entered:\", a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "927ae87b",
   "metadata": {},
   "source": [
    "#### 类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8fccb80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Object created with value 10\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "class myclass:\n",
    "    def __init__(self, x):     #self 代表类的实例，而非类\n",
    "        self.x = x\n",
    "        print(\"Object created with value\", self.x)\n",
    "    def mymethod(self):\n",
    "        print(self.x)\n",
    "\n",
    "obj = myclass(10)\n",
    "obj.mymethod()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42d28bc5",
   "metadata": {},
   "source": [
    "#### 继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "6f872df1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Object created with value 1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "class Class(myclass):\n",
    "    def __init__(self, arg1, arg2):\n",
    "        myclass.__init__(self,arg1)\n",
    "        self.arg2 = arg2\n",
    "v=Class(1,2)\n",
    "v.mymethod()       "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
